package com.hqq.graph;

import java.util.ArrayList;
import java.util.List;

/**
 * LinkedListGraph
 * 基于链表的无向图
 * Created by heqianqian on 2017/8/6.
 */
public class LinkedListGraph implements Graph{

    private final int vertexCount;//顶点个数
    private int edgeCount;//边的个数
    private List<Integer>[] adj;//邻接表

    @SuppressWarnings("unchecked")
    public LinkedListGraph(int vertexCount) {
        this.vertexCount = vertexCount;
        adj = new ArrayList[vertexCount];
        for (int i = 0; i < vertexCount; i++) {
            adj[i] = new ArrayList<>();
        }
    }

    @Override
    public int vertexCount() {
        return this.vertexCount;
    }

    @Override
    public int edgeCount() {
        return this.edgeCount;
    }

    @Override
    public void addEdge(int v, int w) {
        adj[v].add(w);
        adj[w].add(v);
        edgeCount++;
    }

    @Override
    public Iterable<Integer> adjVertex(int v) {
        return adj[v];
    }
}
